using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViewOnCore;
using ViewOnCore.Helper.WPF;
using ViewOnCore.Helper.Collection;
using ViewOnCore.HostContract;
using ViewOnCore.HostContract.Settings;
using ViewOnCore.Visual;

namespace SettingsAddIn
{
    public partial class SettingsPanel : VisualPart
    {

        #region Constructor

        public SettingsPanel()
        {
            InitializeComponent();

            btnOk.Click += new RoutedEventHandler(btnOk_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);

            //----
            Host.SettingsService.SettingPanels.Add(new UI.SettingsPanel.SettingsGeneralControl());
            Host.SettingsService.SettingPanels.Add(new UI.SettingsPanel.SettingsVisualStyleControl());
            Host.SettingsService.SettingPanels.Add(new UI.SettingsPanel.SettingsAssociationsControl());
            Host.SettingsService.SettingPanels.Add(new UI.SettingsPanel.SettingsAutoUpdateControl());
            Host.SettingsService.SettingPanels.Add(new UI.SettingsPanel.SettingsAboutControl());
        }

        #endregion

        #region AddIn : Properties

        override public string Handle
        {
            get
            {
                return "ViewOnTv_Settings";
            }
        }

        override public TagsCollection MediaTypeTags
        {
            get
            {
                return new TagsCollection();
            }
        }

        #endregion

        #region AddSettingPanel

        private void AddSettingPanel(ISettingPanel panel)
        {
            // Tab item
            TabItem item = new TabItem();
            item.Header = panel.Title;

            tabMain.Items.Add(item);

            // Border
            Border border = new Border();
            border.SetResourceReference(Border.BackgroundProperty, "VOGradient01");
            border.CornerRadius = new CornerRadius(2);
            border.HorizontalAlignment = HorizontalAlignment.Stretch;
            border.Margin = new Thickness(5);
            item.Content = border;

            ((UserControl)panel).HorizontalAlignment = HorizontalAlignment.Stretch;
            ((UserControl)panel).Margin = new Thickness(5);
            border.Child = (UIElement)panel;
        }

        #endregion

        #region VisualPart : Management

        override public VisualPartDisplayInformation OnRequestDisplayInformation(VisualPartDisplayInformation currentDisplayInformation, VisualPartDisplayInformation leavedDisplayInformation)
        {
            return new VisualPartDisplayInformation();
        }

        public override void OnPreDisplay(Control container)
        {
            if (tabMain.Items.Count > 0)
                return;

            foreach (ISettingPanel panel in Host.SettingsService.SettingPanels)
                AddSettingPanel(panel);
        }

        override public void OnPostDisplay(Control container)
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                (DelegateInvoker)delegate
            {

                foreach (TabItem tabItem in tabMain.Items)
                {
                    ISettingPanel panel = ((Border)tabItem.Content).Child as ISettingPanel;
                    panel.Reset();
                }

            });
        }

        override public VisualPartPriority OnRequestVisualPartPriority()
        {
            if (Host.VisualService.VisualPartMode != VisualPartMode.Normal)
                return VisualPartPriority.None;

            if (Host.MediaService.CurrentMediaDeviceFeature.MediaTypeTags.Contains("Settings"))
                return VisualPartPriority.Normal;

            return VisualPartPriority.None;
        }

        #endregion

        #region Ok / Cancel

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            foreach (TabItem tabItem in tabMain.Items)
            {
                ISettingPanel panel = ((Border)tabItem.Content).Child as ISettingPanel;
                panel.Apply();
            }

            Host.VisualService.NavigateBack();
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            foreach (TabItem tabItem in tabMain.Items)
            {
                ISettingPanel panel = ((Border)tabItem.Content).Child as ISettingPanel;
                panel.Cancel();
            }

            Host.VisualService.NavigateBack();
        }

        #endregion

    }
}